Technology = {}
TechnologyFunc = {}
TechnologyFunc.__index = TechnologyFunc
setmetatable(Technology, TechnologyFunc)

--[[
UserData = {}
file = io.open("/Users/yuanfei/kuailezhentu/proto/userinfo.json", "r")
UserData.userinfo = cjson.decode(file:read("*a"))
file = io.open("/Users/yuanfei/kuailezhentu/proto/config.json", "r")
UserData.config = cjson.decode(file:read("*a"))
]]

TechnologyFunc.getAllTechnology = function()
  return UserData.config.models.Technology
end

TechnologyFunc.create = function(self, tid)
  local l = {}
  setmetatable(l, TechnologyFunc)
  local techs = Technology:getAllTechnology()
  l.config = techs[tid]
  l.level = UserData.userinfo.host.store.tech[tostring(tid)]
  if l.level==nil then l.level=0 end
  print("TechnologyFunc.create", techs, l.level, l.config)
  return l
end

TechnologyFunc.getUpgradeDuration = function(self)
  local levelConfig = UserData.config.models.TechLevel[self.level+1]
  if self.config.type == "formation" then
    return levelConfig.formSecond 
  else
    return levelConfig.techSecond
  end
end

TechnologyFunc.getUpgradeCash = function(self)
  local levelConfig = UserData.config.models.TechLevel[self.level+1]
  if self.config.type == "formation" then
    return levelConfig.formCash
  else
    return levelConfig.techCash1
  end
end
TechnologyFunc.getUpRequireLevel = function(self)
  local l = (self.level+1) * self.config.blevelIncr
  if l < self.config.level then
    return self.config.level
  end
  return l
end

TechnologyFunc.getContainerForFormation = function(self)
  local count = 2
  for i=3, 5 do
    local tmp = string.format("open%dLevel", i)
    local rl = self.config[tmp]
    if rl>self.level then break end
    count = i
  end
  return count
end

TechnologyFunc.createTip = function(self)
  local tipLayer = CCBReader:load("ScienceTip.ccbi")
  TypeCast(tipLayer:getChildByTag(1010), "CCLabelTTF"):setString(self.config.name)
  local desc = ""
  if self.config.type=="formation" then
    local t = self:getContainerForFormation()
    if t<5 then t=t+1 end
    local rl = self.config[string.format("open%dLevel", t)]
    desc = string.format("%s\n%d级%s可布置%d人", self.config.desc, rl, self.config.name, t)
  else
    desc = string.format("%s\n每级增加%s%d", self.config.desc, self.config.name, self.config.attrIncr) 
  end
  TypeCast(tipLayer:getChildByTag(1011), "CCLabelTTF"):setString(desc)
  
  TypeCast(tipLayer:getChildByTag(1012), "CCLabelTTF"):setString(string.format("科学院等级%d级", self:getUpRequireLevel()))
  TypeCast(tipLayer:getChildByTag(1013), "CCLabelTTF"):setString(string.format("现金%d", self:getUpgradeCash()))
  TypeCast(tipLayer:getChildByTag(1014), "CCLabelTTF"):setString(formatSecond(self:getUpgradeDuration()))

  return tipLayer  
end
--local tmp = Technology:create(9)
--print(tmp.config.name, tmp.level, tmp:getUpgradeDuration())
--local tmp = Technology:getAllTechnology()
--for k,v in pairs(tmp) do
--  print(k,v)
--end
